package cn.yx.common.mdc.configuration;

import cn.yx.common.mdc.spring.MdcThreadPoolTaskExecutor;
import org.springframework.boot.autoconfigure.task.TaskExecutionProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;

/**
 * <p>自定义的SpringBoot异步任务线程池，支持MDC上下文信息。</p>
 * <p>使用@Async("mdcAsyncTaskExecutor")注解时，异步任务中加入MDC上下文信息。</p>
 * <p>使用了SpringBoot自带的TaskExecutionProperties自动化配置类。spring.task.execution</p>
 *
 * @author Wgssmart
 */
@Configuration
@EnableConfigurationProperties(TaskExecutionProperties.class)
@EnableAsync
public class MdcAsyncTaskExecutor {

    private final TaskExecutionProperties taskExecutionProperties;

    public MdcAsyncTaskExecutor(TaskExecutionProperties taskExecutionProperties) {
        this.taskExecutionProperties = taskExecutionProperties;
    }

    @Bean
    public AsyncTaskExecutor mdcAsyncTaskExecutor() {
        return new MdcThreadPoolTaskExecutor(taskExecutionProperties);
    }

}
